//
//  LeftViewController.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/17.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "LeftViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "DoctorApplyController.h"
#import "FocusDoctorController.h"
#import "CaseManageController.h"
#import "SettingViewController.h"
#import "AboutProducController.h"
#import "SharedViewController.h"
#import "SettingViewController.h"
#import "DescShowViewController.h"

@interface LeftViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 没有用户登录界面 */
@property (nonatomic, strong) UIView *noLoginBGView;

/** 用户登录界面 */
@property (nonatomic, strong) UIView *loginBGView;

/** tableView */
@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSArray *arrData1;
@property (nonatomic, strong) NSArray *arrData2;

@end

static NSString *cellID = @"cellID";

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getData];
    
    [self setloginView];
    
}

#pragma mark
#pragma mark - 加载数据
-(void)getData{
    self.arrData1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.arrData2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];
}

#pragma mark
#pragma mark - 搭建登录界面
-(void)setloginView{
    
    //设置头部
    [self setLoginHeadView];
    //设置底部
    [self setBodyView];
    
}

#pragma mark
#pragma mark - 设置用户未登录头部界面
- (void)setUnLoginHeadView{
    
    //创建视图
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
    
    //添加到子View
    [self.view addSubview:headView];
    
    //背景颜色
    headView.backgroundColor = kThemeColor;
    
    //创建ImageView
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    //1.头像
    imgView.image = [UIImage imageNamed:@"name"];
    //添加到
    [headView addSubview:imgView];
    //居中
    imgView.center = CGPointMake(SCREEN_LEFT_WIDTH/2, 50);
    
    //2.登录按钮
    UIButton *loginBtn = [[UIButton alloc] init];
    //设置按钮文字
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //添加监听
    [loginBtn addTarget:self action:@selector(gotoLoginView) forControlEvents:UIControlEventTouchUpInside];
    
    //添加登录按钮
    [headView addSubview:loginBtn];
    //自动布局
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(imgView.mas_bottom).mas_offset(10);
        make.leading.mas_equalTo(headView).mas_offset(40);
        
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    //2.注册按钮
    UIButton *registerBtn = [[UIButton alloc] init];
    //设置按钮文字
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //添加登录按钮
    [headView addSubview:registerBtn];
    //自动布局
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(imgView.mas_bottom).mas_offset(10);
        make.trailing.mas_equalTo(headView).mas_offset(-120);
        
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];

}

#pragma mark
#pragma mark - 设置用户登录头部页面
- (void)setUserLoginHeadView{
    
    //创建视图
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
    
    //添加到子View
    [self.view addSubview:headView];
    
    //背景颜色
    headView.backgroundColor = kThemeColor;
    
    //1.图片按钮
    UIButton *imgBtn = [[UIButton alloc]init];
    
    //点击事件
    [imgBtn addTarget:self action:@selector(descShow) forControlEvents:UIControlEventTouchUpInside];
    
    [headView addSubview:imgBtn];
    [imgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(headView).mas_offset(20);
        make.top.mas_equalTo(headView).mas_equalTo(30);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    
    //1.1创建ImageView
    UIImageView *imgView = [[UIImageView alloc] init];
    
    //切圆角
    imgView.layer.cornerRadius = 35;
    imgView.layer.masksToBounds = YES;
    
    //1.2.头像
    imgView.image = [UIImage imageNamed:@"banner"];
    //添加到
    [imgBtn addSubview:imgView];
    //布局
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(imgBtn);
    }];

    //2.名字
    UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(100, 30, 200, 30)];
    nameL.text = @"乔布斯";
    [self.view addSubview:nameL];
    
    //3.详细信息
    UIButton *descBtn = [[UIButton alloc] initWithFrame:CGRectMake(100, 70, 200, 30)];

    [descBtn setTitle:@"查看个人资料" forState:UIControlStateNormal];
    
    [descBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //文字左对齐
    descBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    //监听
    [descBtn addTarget:self action:@selector(descShow) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:descBtn];
}

#pragma mark
#pragma mark - 点击'头像'或'查看个人资料'
-(void)descShow{
    
    DescShowViewController *showVC = [[DescShowViewController alloc]init];
    
    [self jumpToNextViewController:showVC];
}

#pragma mark
#pragma mark - 设置登录头部视图
- (void)setLoginHeadView{
    
    //读取本地文件信息
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *pwd = [userDefaults objectForKey:@"pwd"];
    NSLog(@"%@", pwd);

    if ([pwd isEqualToString:@"123"]) {
        
        //用户登录完成
        [self setUserLoginHeadView];
    } else{
        
        //用户未登录
        [self setUnLoginHeadView];
    }
    
}

#pragma mark
#pragma mark - 跳转登录画面
-(void)gotoLoginView{
    
    UIViewController *loginVC = [[LoginViewController alloc] init];
    
    [self jumpToNextViewController:loginVC];
}

#pragma mark
#pragma mark - 设置底部视图
- (void)setBodyView{
    
    //tableView
    UITableView *tabV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    //添加视图
    [self.view addSubview:tabV];
    
    //设置代理
    tabV.delegate = self;
    tabV.dataSource = self;
    
    //注册cell
    [tabV registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    //布局
    [tabV mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.top.mas_equalTo(self.view).mas_offset(120);
        make.leading.bottom.mas_equalTo(self.view);
        make.trailing.mas_equalTo(self.view).mas_offset(-[UIScreen mainScreen].bounds.size.width*0.2);
    }];
    
    
}

#pragma mark
#pragma mark - 数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    //设置箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //第一组数据
    if (indexPath.section == 0) {
        cell.textLabel.text = self.arrData1[indexPath.row];
    }else{
        //第二组数据
        cell.textLabel.text = self.arrData2[indexPath.row];
    }
    
    return cell;
    
    
}

#pragma mark
#pragma mark - 代理方法 组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.1;
    }
    return 5;
}

#pragma mark
#pragma mark - 选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    //跳转控制器
    UIViewController *newVC = nil;
    
    if (section == 0 &&  row == 0) {
        newVC = (DoctorApplyController *)[[DoctorApplyController alloc]init];
        
        [self jumpToNextViewController:newVC];
        
        newVC.navigationItem.title = self.arrData1[row];
    }
    
    if (section == 1 &&  row == 0) {
        
        newVC = (SettingViewController *)[[SettingViewController alloc]init];
        
        [self jumpToNextViewController:newVC];
        
        newVC.navigationItem.title = self.arrData2[row];
        
    }
}

@end
